using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Core;

namespace WebViewGenerator.PagesGenerators
{
	public class EditPageFileGenerator
	{
		private const string DIRECTORY = @"GeneratedFiles\Web\Pages\v1\default\Admin\";

		private const string SINGLE_FIELD = 
@"				<dt><label for=""{CLASS_NAME_LOWER}-{FIELD_NAME_LOWER}-<%= this.{CLASS_NAME}.Id %>"">{FIELD_NAME}</label></dt>
				<dd><input id=""{CLASS_NAME_LOWER}-{FIELD_NAME_LOWER}-<%= this.{CLASS_NAME}.Id %>"" value=""<%= this.{CLASS_NAME}.{FIELD_NAME} %>"" class=""required {JS_VALID_TEXT}"" name=""<%= ParameterNames.{CLASS_NAME}.Field.{FIELD_NAME_UPPER} %>"" type=""text"" /></dd>
";

		private string fields = "";

		public EditPageFileGenerator() {}


		public EditPageFileGenerator(Assembly assembly)
		{
			this.Assembly = assembly;
		}


		public Assembly Assembly { get; set; }


		public void Execute(IEnumerable<string> selectedClassNames)
		{
			//read in the template
			TextReader reader = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + @"\TemplateFiles\Pages\EditPageFile.template");

			string template = reader.ReadToEnd();

			TextWriter writer;

			foreach (string fullName in selectedClassNames) {
				//create a new template string
				string classTemplate = template;

				this.fields = "";

				Type classType = this.Assembly.GetType(fullName);
				string className = classType.Name;

				classTemplate = classTemplate.Replace("{CLASS_NAME}", className);
				classTemplate = classTemplate.Replace("{CLASS_NAME_UPPER}", className.ToUpper());
				classTemplate = classTemplate.Replace("{CLASS_NAME_LOWER}", className.ToLower());

				classTemplate = classTemplate.Replace("{MODEL_NAMESPACE}", Utilities.GetModelNameSpace(classType.Namespace));
				classTemplate = classTemplate.Replace("{DAO_NAMESPACE}", Utilities.GetDaoNameSpace(classType.Namespace));
				classTemplate = classTemplate.Replace("{DAO_SUPPORT_NAMESPACE}", Utilities.GetDaoSupportNameSpace(classType.Namespace));
				classTemplate = classTemplate.Replace("{SUPPORT_NAMESPACE}", Utilities.GetSupportNameSpace(classType.Namespace));
				classTemplate = classTemplate.Replace("{SUPPORT_REQUEST_NAMESPACE}", Utilities.GetSupportRequestNameSpace(classType.Namespace));
				classTemplate = classTemplate.Replace("{NAMESPACE}", Utilities.GetLogicNameSpace(classType.Namespace));

				this.ProcessFields(className, classType);

				classTemplate = classTemplate.Replace("{FIELDS}", this.fields);

				Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + DIRECTORY + className);

				writer = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + DIRECTORY + className + "\\Edit.aspx");

				writer.Write(classTemplate);

				writer.Flush();
				writer.Close();
			}
		}


		private void ProcessFields(string className,
								   Type classType)
		{

			foreach (MemberInfo member in classType.GetMembers()) {
				if (member.MemberType == MemberTypes.Property) {
					if (member.Name != "Id" && member.Name != "Version") {
						Type t = Utilities.GetRealTypeFromMemberProperty(member);

						this.fields += SINGLE_FIELD.Replace("{CLASS_NAME}", className)
							.Replace("{CLASS_NAME_LOWER}", className.ToLower())
							.Replace("{FIELD_NAME}", member.Name)
							.Replace("{FIELD_NAME_UPPER}", member.Name.ToUpper())
							.Replace("{FIELD_NAME_LOWER}", member.Name.ToLower())
							.Replace("{JS_VALID_TEXT}", "") + Environment.NewLine;


					}
				}
			}
		}
	}
}